package com.siegelion.repository;

import android.annotation.SuppressLint;

import androidx.lifecycle.MutableLiveData;

import com.siegelion.api.UserService;
import com.siegelion.model.UserResponse;
import com.siegelion.net.BaseObserver;
import com.siegelion.net.NetworkApi;



@SuppressLint("CheckResult")
public class UserRepository {

    private static final String TAG = UserRepository.class.getSimpleName();
    final MutableLiveData<UserResponse> test = new MutableLiveData<>();

    public final MutableLiveData<String> failed = new MutableLiveData<>();

    private static volatile UserRepository mInstance;

    public static UserRepository getInstance() {
        if (mInstance == null) {
            synchronized (UserRepository.class) {
                if (mInstance == null) {
                    mInstance = new UserRepository();
                }
            }
        }
        return mInstance;
    }
    /**
     * 获取新闻数据
     * @return news
     */
    public MutableLiveData<UserResponse> login(String name,String pwd) {
        getTestForNetwork(name,pwd);
        return test;
    }



    /**
     * 从网络获取壁纸数据
     */
    private void getTestForNetwork(String name,String pwd) {
        System.out.println("net");
        NetworkApi.createService(UserService.class, 5).
                login(name,pwd).compose(NetworkApi.applySchedulers(new BaseObserver<UserResponse>() {
            @Override
            public void onSuccess(UserResponse userResponse) {
                System.out.println("net1");
                System.out.println(userResponse);
                if (userResponse.getCode() == 1) {
                    //保存本地数据
                    test.setValue(userResponse);
                } else {
                    failed.postValue("数据为空");
                }
            }

            @Override
            public void onFailure(Throwable e) {
                failed.postValue("News Error: " + e.toString());
            }
        }));
    }


}
